import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

import '../../resource/language/generated/l10n.dart';

class ViewBigVideo extends StatefulWidget {
  var avatarFile, onDelete;

  ViewBigVideo(this.avatarFile, this.onDelete, {super.key});

  @override
  _ViewBigVideoState createState() => _ViewBigVideoState();
}

class _ViewBigVideoState extends State<ViewBigVideo> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.avatarFile)
      ..initialize().then(
        (_) {
          setState(() {
            _controller.play();
          });
        },
      );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xfff6f7f9),
      appBar: AppBar(
        leading: GestureDetector(
          onTap: () => Navigator.pop(context),
          child: const Icon(
            Icons.arrow_back_ios,
            color: Color(0xff333333),
          ),
        ),
        title: const Text(
          '1/1',
          style: TextStyle(
              color: Color(0xff333333),
              fontWeight: FontWeight.w600,
              fontSize: 16),
        ),
        actions: [
          CupertinoButton(
              child: const Icon(
                Icons.more_horiz_rounded,
                color: Color(0xff333333),
              ),
              onPressed: () {
                showModalBottomSheet(
                    context: context,
                    isScrollControlled: true,
                    backgroundColor: const Color(0xffffffff),
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(8))),
                    builder: (_) {
                      return SafeArea(
                        bottom: true,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Container(
                              height: 1,
                              color: const Color(0xfff5f5f5),
                            ),
                            InkWell(
                              onTap: () async {
                                Navigator.pop(context);
                                bool result =
                                    await widget.onDelete!(widget.avatarFile);
                                if (result) {
                                  Navigator.pop(context);
                                }
                              },
                              child: Container(
                                height: 52,
                                alignment: Alignment.center,
                                child: Text(
                                  T.current.delete,
                                  style: TextStyle(
                                      fontSize: 17,
                                      fontWeight: FontWeight.w400,
                                      color: Color(0xff000000)),
                                ),
                              ),
                            ),
                            Container(
                              height: 10,
                              color: const Color(0xfff5f5f5),
                            ),
                            GestureDetector(
                              onTap: () => Navigator.of(context).pop(),
                              child: Container(
                                height: 52,
                                alignment: Alignment.center,
                                child: Text(
                                  T.current.cancel,
                                  style: TextStyle(
                                      fontSize: 17,
                                      fontWeight: FontWeight.w400,
                                      color: Color(0xff000000)),
                                ),
                              ),
                            )
                          ],
                        ),
                      );
                    });
              })
        ],
        centerTitle: true,
        backgroundColor: const Color(0xffffffff),
        elevation: 0,
      ),
      body: Center(
        child: _controller.value.isInitialized
            ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              )
            : Container(),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}
